<?php
//get condition
if ($task == 'blogsection'){
	$where = " AND sectionid=".$id;
}	
if ($task == 'blogcategory'){
	$sql = "select * from #__categories where parent_id=".$id;
	$database->setQuery($sql);
	$lsRows = $database->loadObjectList();
	if (count($lsRows)>0){
		$arr = "(".$id.",";
		for ($i=0; $i<count($lsRows); $i++){
			if ($i==(count($lsRows)-1)){
				$str = "";
			}else{
				$str = ",";
			}
			$arr .= $lsRows[$i]->id.$str;
		}
		$arr .= ")";
		//echo $arr;
		$where = " AND catid IN".$arr;
	}else{
		$where = " AND catid=".$id;
	}
}	
	
$sql = "SELECT * FROM #__news WHERE state=1".$where;
$database->setQuery($sql);
$rows = $database->loadObjectList();

$nows = mktime(date("H"),date("i"),date("s"),date("m"),date("d"),date("Y"));
$arr = "(";
$notArr = "(";
for ($i=0; $i<count($rows); $i++){
	if ($i == (count($rows)-1) || count($rows)==1) {
		$str = "";
	}else{
		$str = ",";
	}
	$end_date = mktime(unFormatdate($rows[$i]->end_date,"H"),unFormatdate($rows[$i]->end_date,"i"),unFormatdate($rows[$i]->end_date,"s"),unFormatdate($rows[$i]->end_date,"m"),unFormatdate($rows[$i]->end_date,"d"),unFormatdate($rows[$i]->end_date,"Y"));				
	$days = ($end_date - $nows)/86400;
	if ($days>0){
		$arr .= $rows[$i]->id.$str;
		$notArr .= "";
	}else{
		$arr .= "";
		$notArr .= $rows[$i]->id.$str;
	}
}
$arr .= ")";
$notArr .= ")";

//set state in notArr unpublish
$bodytag = str_replace("(", "", $notArr);
$bodytag = str_replace(")", "", $bodytag);
if($bodytag != '') mysql_query("UPDATE ".$mosConfig_dbprefix."news SET state=0 WHERE id IN".$notArr); 

//path image
$path = $mosConfig_live_site."/uploads/news";
//query 
$sql = "SELECT * FROM #__news WHERE id IN".$arr." ORDER BY created DESC, id DESC";
$database->setQuery($sql);
$lsKM = $database->loadObjectList();
if (count($lsKM)>0){
?>
<div class="w_boxright clearfix">
	<div class="w_title_boxright"><span>Chương trình khuyến mại</span></div>
	<?php
	for ($i=0; $i<count($lsKM); $i++){
	$link = sefRelToAbs("index.php?option=com_news&task=view&id=".$lsKM[$i]->id).$lsKM[$i]->title_alias;	
	if ($i == (count($lsKM)-1))	$styleBorder = ' style="border-bottom:0;"';
	else $styleBorder = '';
	
	//Tinh thoi gian
	$starttime=time();
	$endtime=strtotime($lsKM[$i]->end_date);
	 
	$timediff = $endtime-$starttime;
	   $days=intval($timediff/86400);
	   $remain=$timediff%86400;
	   $hours=intval($remain/3600);
	   $remain=$remain%3600;
	   $mins=intval($remain/60);
	   $secs=$remain%60;
	//$timediff=$days.'days'.$hours.'hr'.$mins.'min'.$secs.'s';   
	?>
	<div class="w_news"<?php echo $styleBorder;?>>
		<a href="<?php echo $link;?>" class="screenshot" rel="<?php echo $path;?>/medium/<?php echo $lsKM[$i]->mediumImage;?>">
			<img src="<?php echo $path;?>/thumbnail/<?php echo $lsKM[$i]->thumbnailImage;?>" width="80" alt="" border="0" />
		</a>
		<a href="<?php echo $link;?>"><?php echo $lsKM[$i]->title;?></a>
		<p><?php echo mb_substr($lsKM[$i]->introtext, 0, 150, "utf-8")."...";?></p>
		<p class="datetime">Ngày bắt đầu: <?php echo unFormatDate($lsKM[$i]->start_date,"d/m/Y H:i:s");?></p>
		<p class="datetime">Ngày kết thúc: <?php echo unFormatDate($lsKM[$i]->end_date,"d/m/Y H:i:s");?></p>
		<div>
			<span>Thời gian còn lại: </span>
			<span class="clearfix"></span>
			<span class="timer"><?php echo $timediff=$days.' ngày, '.$hours.' giờ, '.$mins.' phút';?></span>
			<span class="clearfix"></span>
		</div>
	</div>
	<div class="clearfix"></div>
	<?php }?>
</div>
<div class="clearfix"></div>
<?php }?>